﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace CoCaNgua
{
    public partial class Knight : Control
    {
        private int _currentPos;
        private Color _colour;
        private Player _myboss;
        private int _number;
        private Point _home;
        
        public Point Home
        {
            get { return _home; }
            set { _home = value; }
        }

        public int Number
        {
            get { return _number; }
            set { _number = value; }
        }

        private int _id;

        public int Id
        {
            get { return _id; }
            set { _id = value; }
        }

        internal Player Myboss
        {
            get { return _myboss; }
            set { _myboss = value; }
        }
        

        public Color Colour
        {
            get { return _colour; }
            set { _colour = value; }
        }

        public int CurrentPos
        {
            get { return _currentPos; }
            set { _currentPos = value; }
            
        }

        #region MyRegion
        private Point[][] roadData = {new Point[]{
                                   new Point(200,200),
                                   new Point(144,224),
                                   new Point(144,208),
                                   new Point(144,192),
                                   new Point(144,176),
                                   new Point(144,160),
                                   new Point(144,144),
                                   new Point(160,144),
                                   new Point(176,144),
                                   new Point(192,144),
                                   new Point(208,144),
                                   new Point(224,144),
                                   new Point(224,112),
                                   new Point(224,80),
                                   new Point(208,80),
                                   new Point(192,80),
                                   new Point(176,80),
                                   new Point(160,80),
                                   new Point(144,80),
                                   new Point(144,64),
                                   new Point(144,48),
                                   new Point(144,32),
                                   new Point(144,16),
                                   new Point(144,0),
                                   new Point(112,0),
                                   new Point(80,0),
                                   new Point(80,16),
                                   new Point(80,32),
                                   new Point(80,48),
                                   new Point(80,64),
                                   new Point(80,80),
                                   new Point(64,80),
                                   new Point(48,80),
                                   new Point(32,80),
                                   new Point(16,80),
                                   new Point(0,80),
                                   new Point(0,112),
                                   new Point(0,144),
                                   new Point(16,144),
                                   new Point(32,144),
                                   new Point(48,144),
                                   new Point(64,144),
                                   new Point(80,144),
                                   new Point(80,160),
                                   new Point(80,176),
                                   new Point(80,192),
                                   new Point(80,208),
                                   new Point(80,224),
                                   new Point(112,224),
                                   new Point(112,208),
                                   new Point(112,192),
                                   new Point(112,176),
                                   new Point(112,160),
                                   new Point(112,144)
                               },
                               new Point[]{
                                   new Point(200,40),
                                   new Point(224,80),
                                   new Point(208,80),
                                   new Point(192,80),
                                   new Point(176,80),
                                   new Point(160,80),
                                   new Point(144,80),
                                   new Point(144,64),
                                   new Point(144,48),
                                   new Point(144,32),
                                   new Point(144,16),
                                   new Point(144,0),
                                   new Point(112,0),
                                   new Point(80,0),
                                   new Point(80,16),
                                   new Point(80,32),
                                   new Point(80,48),
                                   new Point(80,64),
                                   new Point(80,80),
                                   new Point(64,80),
                                   new Point(48,80),
                                   new Point(32,80),
                                   new Point(16,80),
                                   new Point(0,80),
                                   new Point(0,112),
                                   new Point(0,144),
                                   new Point(16,144),
                                   new Point(32,144),
                                   new Point(48,144),
                                   new Point(64,144),
                                   new Point(80,144),
                                   new Point(80,160),
                                   new Point(80,176),
                                   new Point(80,192),
                                   new Point(80,208),
                                   new Point(80,224),
                                   new Point(112,224),
                                   new Point(144,224),
                                   new Point(144,208),
                                   new Point(144,192),
                                   new Point(144,176),
                                   new Point(144,160),
                                   new Point(144,144),
                                   new Point(160,144),
                                   new Point(176,144),
                                   new Point(192,144),
                                   new Point(208,144),
                                   new Point(224,144),
                                   new Point(224,112),
                                   new Point(208,112),
                                   new Point(192,112),
                                   new Point(176,112),
                                   new Point(160,112),
                                   new Point(144,112)
                               },
                               new Point[]{
                                   new Point(40,40),
                                   new Point(80,0),
                                   new Point(80,16),
                                   new Point(80,32),
                                   new Point(80,48),
                                   new Point(80,64),
                                   new Point(80,80),
                                   new Point(64,80),
                                   new Point(48,80),
                                   new Point(32,80),
                                   new Point(16,80),
                                   new Point(0,80),
                                   new Point(0,112),
                                   new Point(0,144),
                                   new Point(16,144),
                                   new Point(32,144),
                                   new Point(48,144),
                                   new Point(64,144),
                                   new Point(80,144),
                                   new Point(80,160),
                                   new Point(80,176),
                                   new Point(80,192),
                                   new Point(80,208),
                                   new Point(80,224),
                                   new Point(112,224),
                                   new Point(144,224),
                                   new Point(144,208),
                                   new Point(144,192),
                                   new Point(144,176),
                                   new Point(144,160),
                                   new Point(144,144),
                                   new Point(160,144),
                                   new Point(176,144),
                                   new Point(192,144),
                                   new Point(208,144),
                                   new Point(224,144),
                                   new Point(224,112),
                                   new Point(224,80),
                                   new Point(208,80),
                                   new Point(192,80),
                                   new Point(176,80),
                                   new Point(160,80),
                                   new Point(144,80),
                                   new Point(144,64),
                                   new Point(144,48),
                                   new Point(144,32),
                                   new Point(144,16),
                                   new Point(144,0),
                                   new Point(112,0),
                                   new Point(112,16),
                                   new Point(112,32),
                                   new Point(112,48),
                                   new Point(112,64),
                                   new Point(112,70)
                               } ,
                               new Point[]{
                                   new Point(40,200),
                                   new Point(0,144),
                                   new Point(16,144),
                                   new Point(32,144),
                                   new Point(48,144),
                                   new Point(64,144),
                                   new Point(80,144),
                                   new Point(80,160),
                                   new Point(80,176),
                                   new Point(80,192),
                                   new Point(80,208),
                                   new Point(80,224),
                                   new Point(112,224),
                                   new Point(144,224),
                                   new Point(144,208),
                                   new Point(144,192),
                                   new Point(144,176),
                                   new Point(144,160),
                                   new Point(144,144),
                                   new Point(160,144),
                                   new Point(176,144),
                                   new Point(192,144),
                                   new Point(208,144),
                                   new Point(224,144),
                                   new Point(224,112),
                                   new Point(224,80),
                                   new Point(208,80),
                                   new Point(192,80),
                                   new Point(176,80),
                                   new Point(160,80),
                                   new Point(144,80),
                                   new Point(144,64),
                                   new Point(144,48),
                                   new Point(144,32),
                                   new Point(144,16),
                                   new Point(144,0),
                                   new Point(112,0),
                                   new Point(80,0),
                                   new Point(80,16),
                                   new Point(80,32),
                                   new Point(80,48),
                                   new Point(80,64),
                                   new Point(80,80),
                                   new Point(64,80),
                                   new Point(48,80),
                                   new Point(32,80),
                                   new Point(16,80),
                                   new Point(0,80),
                                   new Point(0,112),

                                   new Point(16,112),
                                   new Point(32,112),
                                   new Point(48,112),
                                   new Point(64,112),
                                   new Point(70,112)
                               }
                                 }; 
        #endregion
        private Point[] _road;

        public Point[] Road
        {
            get { return _road; }
            set { _road = value; }
        }

        public Knight()
        {
            InitializeComponent();
            CurrentPos = 0;
            Colour = Color.White;
            this.Width = 16;
            this.Height = 16;
        }

        public Knight(Color c, int id):this()
        {
            //Knight();
            Colour = c;
            this.Id = id;

            if (Colour == Color.Green)
            {
                Road = roadData[0];
                Number = 0;
            }
            else if (Colour == Color.Red)
            {
                Road = roadData[1];
                Number = 1;
            }
            else if (Colour == Color.Blue)
            {
                Road = roadData[2];
                Number = 2;
            }
            else if (Colour == Color.Yellow)
            {
                Road = roadData[3];
                Number = 3;
            }
            Home = Road[0];
        }

        protected override void OnPaint(PaintEventArgs pe)
        {
            // TODO: Add custom paint code here

            // Calling the base class OnPaint
            this.Left = Road[CurrentPos].X; this.Top = Road[CurrentPos].Y;
            pe.Graphics.FillEllipse(new SolidBrush(Colour), new Rectangle(0,0, 16, 16));
            base.OnPaint(pe);
        }
        
        public void Move(int steps)
        {
            
            for (int i = 0; i < steps; i++)
            {
                Thread.Sleep(300);
                CurrentPos += (1);
                this.Invalidate();
                Parent.Update();
            }            
        }

        private bool KiemTraDa(int iValue, int iID, int iNum)
        {
            for (int i = 0; i <= 3; i++)
            {
                if (i != iNum)
                {
                    for (int j = 0; j <= 3; j++)
                    {
                        if (Form1._g.Players[i].Knights[j].CurrentPos > 0)// ko xe't nhung con o nha`
                        {
                            // vi tri tren toan bo ban co max = 48
                            // 1 bat dau tu green
                            int iPosEnemy = Form1._g.Players[i].Knights[j].CurrentPos + i * 12;
                            if (iPosEnemy > 48)
                                iPosEnemy -= 48;
                            int iPosMy = iValue + Form1._g.Players[iNum].Knights[iID].CurrentPos + iNum * 12;
                            if (iPosMy > 48)
                                iPosMy -= 48;
                            if (iPosMy == iPosEnemy)
                            {
                                Form1._g.Players[i].Knights[j].CurrentPos = 0;
                                Form1._g.Players[i].Knights[j].Invalidate();
                                MessageBox.Show("Co a'n ma.ng");                                
                                return true;
                            }
                        }
                    }
                }
            }

            return false;
        }

        private bool ChuyenLuot(int iValue, int iNum)
        {
            for (int i = 0; i < 4; i++)            
                if(i != iNum)
                    for(int j = 0; j < 4; j++)
                        if (KiemTraNcDi(iValue, j, i) == true)
                            return false;
            
            return true;
        }

        public String DatLuot(int iNum)
        {
            Form1._g.Players[iNum].bTurn = false;
            // active cho nguoi choi ke
            int index = iNum + 1;
            if (index >= 4)
                index = 0;            
            Form1._g.Players[index].bTurn = true;
            
            String s = "";
            if (index == 0)
                s = "Green go...";
            else if (index == 1)
                s = "Red go...";
            else if (index == 2)
                s = "Blue go...";
            else
                s = "Yellow go...";

            //Form1.GanThongBao(s);
            CoCaNgua.Program.frmMain.GanThongBao(s);
            //MessageBox.Show(s);
            //CoCaNgua.Form1.lbWarning.Text = s;
            return s;
        }

        private bool KiemTraNcDi(int iValue, int id, int iNum)
        {
            /*
            MessageBox.Show(Form1._g.Players[1].Knights[0].CurrentPos.ToString());
            MessageBox.Show(Form1._g.Players[1].Knights[1].CurrentPos.ToString());
            /**/

            // Kiem tra da den luoc minh chua
            if (Form1._g.Players[iNum].bTurn == false)
                return false;

            if (CurrentPos == 0)
                if (iValue == 1 || iValue == 6)
                    return true;
                else
                    return false;

            for (int i = 0; i <= 3; i++)
            {                
                // Ko co' ba.n o muon den
                //int iCurrPosFri = Form1._g.Players[0].Knights[i].CurrentPos;
                int iCurrPosFri = Form1._g.Players[iNum].Knights[i].CurrentPos;
                if (i != id && CurrentPos < 49)                
                    if (((iValue + CurrentPos) >= iCurrPosFri) && 
                        (iCurrPosFri != 0) && (CurrentPos < iCurrPosFri))
                        return false;                                    
            }

            // co' the xuat chuog
            if (iValue == 1 || iValue == 6)
            {
                if (Form1._g.Players[iNum].Knights[id].CurrentPos == 0)
                {
                    for (int i = 0; i <= 3; i++)
                        if (i != id)
                            if (Form1._g.Players[iNum].Knights[i].CurrentPos == 1)
                                return false;
                    return true;
                }
            }

            // kiem tra co` ba.n ca?n nc di
            for (int i = 0; i <= 3; i++)
            {
                if (i != iNum)
                {
                    for(int j = 0; j <= 3; j++)
                    {
                        if (Form1._g.Players[i].Knights[j].CurrentPos > 0)// ko xe't nhung con o nha`
                        {   
                            // vi tri tren toan bo ban co max = 48
                            // 1 bat dau tu green
                            int iPosEnemy = Form1._g.Players[i].Knights[j].CurrentPos + i * 12;
                            if (iPosEnemy > 48)
                                iPosEnemy -= 48;
                            int iPosMy = Form1._g.Players[iNum].Knights[Id].CurrentPos + iNum * 12;
                            if (iPosMy > 48)
                                iPosMy -= 48;
                            if (((iValue + iPosMy) > iPosEnemy) &&
                                (iPosMy < iPosEnemy))
                                return false;
                        }
                    }
                }                
            }

            // Kiem tra ve chuong vi tri 48
            if ((iValue + CurrentPos) > 48 && (CurrentPos < 48))
                return false;
            // Ngua chuan bi len chuong
            if ((CurrentPos == 48) && (iValue == 6))
                return false;
            // Ngua da len chuong
            if (CurrentPos > 48)
                if ((CurrentPos - 48 + 1) == iValue)
                    return true;
                else
                    return false;

            return true;
        }       

        private void Knight_Click(object sender, EventArgs e)
        {
            // id bat dau tu 0.
            String sResult = "";
            int iValue = Form1._g.MyDice.Value;
                        
            if (Home == Road[CurrentPos])
            {
                if (iValue == 1 || iValue == 6)
                {
                    if (KiemTraNcDi(1, Id, Number) == true)
                    {
                        KiemTraDa(iValue, Id, Number);
                        Move(1);                        
                    }
                }
            }
            else
            {
                if (KiemTraNcDi(iValue, Id, Number) == true)
                {
                    KiemTraDa(iValue, Id, Number);
                    if (CurrentPos > 48)
                        Move(1);
                    else
                        Move(iValue);
                    
                    sResult = DatLuot(Number);                    
                }                    
            }

            // Kiem tra chuyen luot
            // Can kiem tra co dc di ko, de chuyen luot(ko dam choc bay ba)
            // if (KiemTraNcDi(iValue, Id, Number) == true) // chua kiem tra con trong chuong, chua dam dung cai nay
                if (ChuyenLuot(iValue, Number) == true && sResult == "")
                    DatLuot(Number);
            /**/
            if(iValue == 1 || iValue == 6)
                if (Number == 0)
                    sResult = DatLuot(3);
                else
                    sResult = DatLuot(Number - 1);
        }
    }
}
